#include"DEMOSprite.h"
#include"..\..\..\include\hge.h"
#include"..\..\..\include\hgesprite.h"

#include<assert.h>

#include<iostream>
using namespace std;

DEMOSprite::DEMOSprite(char* texName,char* sprDataName)
{
	_hge = hgeCreate(HGE_VERSION);
	_tex = _hge->Texture_Load(texName);

	assert( _tex != NULL);
	
	LoadAuroraSprite(sprDataName);
}

DEMOSprite::~DEMOSprite()
{
	_hge->Texture_Free(_tex);
	_tex = NULL;
}

void DEMOSprite::render2D(int u,int v,int ul,int vl,int x,int y,
					float rot,float scale,DWORD col,bool bTrans,
					float hotSpotX,float hotSpotY)	const
{
	hgeSprite spr(_tex,u,v,ul,vl);

	spr.SetHotSpot(hotSpotX,hotSpotY);

	if(bTrans)
	{
		spr.SetBlendMode( BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);	//	all alpha ed.
	}
	else
	{
		spr.SetBlendMode(BLEND_DEFAULT);
	}
	
	spr.SetColor(col);
	spr.RenderEx(x,y,rot,scale,scale);
}

void DEMOSprite::paintModule(int curModuleIdx,int x,int y,int ox,int oy,
							float rot,float scale,DWORD col,bool bTrans) const
{
	SpriteModule curModule = _allModules[curModuleIdx];
	/*render2D( curModule._u,curModule._v,curModule._width,curModule._height,
				x + ox,y + oy,rot,scale,col);*/
	render2D( curModule._u,curModule._v,curModule._width,curModule._height,
				x + ox,y + oy,rot,scale,col,false,-ox,-oy);
}


void DEMOSprite::paintFrame(int curFrameIdx,int x,int y,int ox,int oy,
							float rot,float scale,DWORD col,bool bTrans ) const
{
	SpriteFrame curFrame = _allFrames[curFrameIdx];

	for(vector<SpriteFModule>::iterator it = curFrame._allFModules.begin(); it != curFrame._allFModules.end(); it++)
	{
		paintFModule( (*it)._moduleId,x + ox,y + oy,(*it)._ox,(*it)._oy,rot,scale,col);
	}
}

void DEMOSprite::paintFModule(string curModuleId,int x,int y,int ox,int oy,
							float rot,float scale,DWORD col,bool bTrans) const
{
	int iModulesCount = _allModules.size();
	for(int i = 0; i < iModulesCount;i++)
	{
		if( _allModules[i]._moduleId == curModuleId)
		{
			paintModule( i, x, y, ox, oy,rot,scale,col);
			return;
		}
	}
}

void DEMOSprite::paintAFrame(string curFrameId,int x,int y,int ox,int oy,
								float rot,float scale,DWORD col,bool bTrans) const
{
	int iFrameCount = _allFrames.size();

	for( int i = 0; i < iFrameCount; i ++ )
	{
		if( _allFrames[i]._frameId == curFrameId)
		{
			paintFrame( i , x, y, ox, oy, rot,scale,col);
			return;
		}
	}
}